#include "DSP2833x_Device.h"     // DSP2833x Headerfile Include File
#include "DSP2833x_Examples.h"   // DSP2833x Examples Include File

#include "DSP2833x_Timer.h"

// Config Timer
void ConfigTimer(enum Timer_Group TimerGroup, float Freq, float Period,PINT TimerInt){
	struct CPUTIMER_VARS *Timer;
	
	switch(TimerGroup){
		case Timer0:
			Timer = &CpuTimer0;
			// PIE Vector table for timer0
			EALLOW;
			PieVectTable.TINT0	= TimerInt;
			EDIS;
			// Enable Interrupt for Group 1, bit 7
			PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
			IER |= M_INT1; 	
			break;
		case Timer1:
			Timer = &CpuTimer1;
			break;
		case Timer2:
			Timer = &CpuTimer2;
			break;
		default:
			Timer = &CpuTimer0;			
	};
	// For timer0	
	// Initialize all timers
	InitCpuTimers();	
	// Config Timer
	ConfigCpuTimer(Timer,Freq,Period);	
	// Start Timer
	Timer->RegsAddr->TCR.all = 0x4001; // Use write-only instruction to set TSS bit = 0	
}

// Config only Timer Period
void ConfigTimerPeriod(enum Timer_Group TimerGroup, float Freq, float Period){
	struct CPUTIMER_VARS *Timer;
	Uint32  temp;
	// Determine which timer
	switch(TimerGroup){
		case Timer0:
			Timer = &CpuTimer0;
			break;
		case Timer1:
			Timer = &CpuTimer1;
			break;
		case Timer2:
			Timer = &CpuTimer2;
			break;
		default:
			Timer = &CpuTimer0;			
	};	
	// Initialize timer period:
    Timer->CPUFreqInMHz = Freq;
    Timer->PeriodInUSec = Period;
    temp = (long) (Freq * Period);
    Timer->RegsAddr->PRD.all = temp;	
}



